<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { BatchMaterialLoadingModel } from '#/api/production/models';

import { Page } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  getBatchMaterialLoadingPagedListApi,
  unloadBatchMaterialLoadingApi,
} from '#/api/production/batchMaterialLoading';

import { useColumns, useSearchSchema } from './data';

/**
 * 下料
 * @param row
 */
function onUnload(row: BatchMaterialLoadingModel) {
  const hideLoading = message.loading({
    content: `正在下料 ${row.barcode}`,
    duration: 0,
    key: 'action_process_msg',
  });
  unloadBatchMaterialLoadingApi(row.id)
    .then(() => {
      message.success({
        content: `下料成功 ${row.barcode}`,
        key: 'action_process_msg',
      });
      refreshGrid();
    })
    .catch(() => {
      hideLoading();
    });
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({
  code,
  row,
}: OnActionClickParams<BatchMaterialLoadingModel>) {
  switch (code) {
    case 'unload': {
      onUnload(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    collapsed: true,
    schema: useSearchSchema(),
    // 控制表单是否显示折叠按钮
    showCollapseButton: true,
    submitOnEnter: true,
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    border: true,
    stripe: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getBatchMaterialLoadingPagedListApi({
            pageNumber: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
  } as VxeTableGridOptions,
});

/**
 * 刷新表格
 */
function refreshGrid() {
  gridApi.query();
}
</script>

<template>
  <Page auto-content-height>
    <Grid />
  </Page>
</template>
